import store from '../../store';
let permission = {
  inserted(el, binding) {
    const { value } = binding;
    const { roles } = store.getters;
    if (value && Array.isArray(value) && value.length > 0) {
      const permissionRoles = new Set(value);
      const userRoles = new Set(roles); 
      const hasPermission = [...userRoles].some((role) =>
        permissionRoles.has(role)
      ); 
      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el);
      }
    } else {
      throw new Error(`使用方式： v-permission="['admin','editor']"`);
    }
  },
};

const install = function (Vue) {
  Vue.directive('permission', permission);
};

if (window.Vue) {
  window['permission'] = permission;
  Vue.use(install); // eslint-disable-line
}

permission.install = install;
export default permission;
